#define _CRT_SECURE_NO_WARNINGS

#include "Queue.h"

void QueueInit(Queue* q)
{
	assert(q);

	q->front = NULL;
	q->rear = NULL;
	q->size = 0;
}
void QueuePush(Queue* q, QDataType data)
{
	assert(q);

	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newNode->data = data;
	newNode->next = NULL;

	if (q->rear == NULL) 
	{
		q->front = q->rear = newNode;
	}
	else
	{
		q->rear->next = newNode;
		q->rear = newNode;
	}
	q->size++;
}
void QueuePop(Queue* q)
{
	assert(q && !QueueEmpty);

	if (q->front->next == NULL)
	{
		free(q->front);
		q->front = q->rear = NULL;
	}
	else
	{
		QNode* del = q->front;
		q->front = q->front->next;

		free(del);
	}

	q->size--;
}
QDataType QueueFront(Queue* q)
{
	assert(q && !QueueEmpty);

	return q->front->data;
}
QDataType QueueBack(Queue* q)
{
	assert(q && !QueueEmpty);

	return q->rear->data;
}
int QueueSize(Queue* q)
{
	return q->size;
}
int QueueEmpty(Queue* q)
{
	assert(q);

	return q->front == NULL && q->rear == NULL;
}
void QueueDestroy(Queue* q)
{
	assert(q);

	QNode* cur = q->front;
	while (cur)
	{
		QNode* del = cur;
		cur = cur->next;

		free(del);
	}

	q->front = q->rear = NULL;
	q->size = 0;
}
